﻿//-----------------------------------------------------------------------
// <copyright file="MarketplaceHubTask.cs" company="In The Hand Ltd">
//     Copyright © 2013-14 In The Hand Ltd. All rights reserved.
// </copyright>
//-----------------------------------------------------------------------

using System;

namespace InTheHand.Phone.Tasks
{
    /// <summary>
    /// Allows an application to launch the Store client application.
    /// </summary>
    public sealed class MarketplaceHubTask
    {
        /// <summary>
        /// Shows the Store client application. 
        /// </summary>
        public void Show()
        {
            Windows.System.Launcher.LaunchUriAsync(new Uri("ms-windows-store:"));
        }
    }

    /// <summary>
    /// Allows an application to launch the Store client application and display the details page for the specified product.
    /// </summary>
    public sealed class MarketplaceDetailTask
    {
        /// <summary>
        /// Gets or sets the unique identifier for the product to be displayed.
        /// </summary>
        public string ContentIdentifier
        {
            get;
            set;
        }

        /// <summary>
        /// Shows the Store client application and displays the details page for the specified product. 
        /// </summary>
        public void Show()
        {
            string contentIdentifier = ContentIdentifier;

            if (string.IsNullOrEmpty(contentIdentifier))
            {
                contentIdentifier = Windows.ApplicationModel.Package.Current.Id.FamilyName;
            }

            Windows.System.Launcher.LaunchUriAsync(new Uri(string.Format("ms-windows-store:PDP?PFN={0}", contentIdentifier)));
        }
    }

    public sealed class MarketplaceReviewTask
    {
        /// <summary>
        /// Shows the Store client application and displays the review page for the specified product.
        /// </summary>
        public void Show()
        {
            string familyName = Windows.ApplicationModel.Package.Current.Id.FamilyName;
            Windows.System.Launcher.LaunchUriAsync(new Uri(string.Format("ms-windows-store:REVIEW?PFN={0}", familyName)));
        }
    }

    /// <summary>
    /// Allows an application to launch the Store client application and display the search results from the specified search terms.
    /// </summary>
    public sealed class MarketplaceSearchTask
    {
        /// <summary>
        /// Gets or sets the search terms.
        /// </summary>
        /// <remarks>The search terms string must be non-null and non-empty or an exception will be thrown.</remarks>
        public string SearchTerms
        {
            get;
            set;
        }

        /// <summary>
        /// Shows the Store client application and displays the search results from the specified search terms.
        /// </summary>
        public void Show()
        {
            if(string.IsNullOrEmpty(SearchTerms))
            {
                throw new ArgumentNullException("SearchTerms");
            }

            Windows.System.Launcher.LaunchUriAsync(new Uri(string.Format("ms-windows-store:Search?query={0}", Uri.EscapeDataString(SearchTerms))));
        }
    }
}
